home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / BORL_TIP / TI1000 / TI1203.ASC < prev    next >
Text File  |  1992-12-11  |  13KB  |  463 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland Pascal/Turbo Pascal           NUMBER  :  1203
  9.   VERSION  :  7.0
  10.        OS  :  All
  11.      DATE  :  December 11, 1992                        PAGE  :  1/7
  12.  
  13.     TITLE  :  Using Validators in OWL Applications
  14.  
  15.  
  16.  
  17.  
  18.        Starting with version 7.0, Borland Pascal introduced
  19.   Validator objects for Turbo Vision and the Object Windows
  20.   Library. These objects help guide the user when they are entering
  21.   data in Edit fields. The purpose of this TI is to introduce the
  22.   user to the idea of using Validators in OWL applications.
  23.        Validators are very flexible objects which can play several
  24.   different roles in your program. A typical validator might ensure
  25.   that the user can only enter numbers within a certain range into
  26.   a particular field. Another common use for Validators might be to
  27.   ensure that users enter stock numbers that begin with two
  28.   alphanumeric characters, then include a dash followed by four
  29.   numbers. An example of this type of stock number might look like
  30.   this: WD-3344. These are only two examples of the many different
  31.   types of validators you can create for your programs.
  32.        Validators are extremely easy to use. In most cases you only
  33.   need to add one line to your code. Suppose you want to add a
  34.   validator to an edit field which will ensure that the user can
  35.   only enter numbers between five and eight. First you would
  36.   initialize your edit field, just as you have always done:
  37.  
  38.       AEdit := New(PEdit, InitResource(@Self, Cm_NumEdit, 50));
  39.  
  40.   The next step is to initialize the validator. The following line
  41.   of code turns the trick:
  42.  
  43.      AEdit^.SetValidator(New(PRangeValidator, Init(5, 8)));
  44.  
  45.   That's all you need to do. Now the only characters the user can
  46.   enter into the control will have to fall within the specified
  47.   range.
  48.        When compiled, the program and resource script included in
  49.   this Tech Info Sheet help demonstrate many of the primary
  50.   features of the validator unit. Included in the example are two
  51.   range validators, four picture validators and one custom
  52.   validator. The range validators ensure that input falls inside a
  53.   particular numeric range. One of the picture validators helps
  54.   guide the user while he enters a date, and another ensures that
  55.   the user follows a valid format while entering a social security
  56.   number. The other validators guide the user while he enters a
  57.   stock number and a special entry that must begin with the letters
  58.   'MIKE' followed by a single number. In order to show you how to
  59.   create your own Validators,  we include a descendant of
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland Pascal/Turbo Pascal           NUMBER  :  1203
  75.   VERSION  :  7.0
  76.        OS  :  All
  77.      DATE  :  December 11, 1992                        PAGE  :  2/7
  78.  
  79.     TITLE  :  Using Validators in OWL Applications
  80.  
  81.  
  82.  
  83.  
  84.   TPxPictureValidator called TSamValidator which gives you an idea
  85.   of how to control input on a character by character basis.
  86.        Validators usually work on a letter by letter or field by
  87.   field basis. That is, they will check a user's input either as he
  88.   enters each character, or later on, when he exits a field. If you
  89.   want to check the data only when the user closes an entire
  90.   screenful of data, then you should examine the TMyEdit object,
  91.   presented below. In this object, we override the TEdit methods
  92.   that would normally ensure that a user's input was checked as he
  93.   leaves a field. As a result, the data is checked only when the
  94.   user closes the dialog in which the edit fields reside.
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.   {************************************************}
  102.   {                                                }
  103.   {   Turbo Pascal 7.0 for Windows                 }
  104.   {   Demo program                                 }
  105.   {   Copyright (c) 1992 by Borland International  }
  106.   {                                                }
  107.   {************************************************}
  108.  
  109.   program ValidExp;
  110.  
  111.   uses WinTypes, WinProcs, OWindows, ODialogs, Strings, Validate,
  112.   BWCC;
  113.  
  114.   {$R ValidExp}
  115.  
  116.   const
  117.     Cm_Dialog    = 101;
  118.     Cm_NumEdit   = 102;
  119.     Cm_NumEdit2  = 103;
  120.     Cm_DateEdit  = 104;
  121.     Cm_StockEdit = 105;
  122.     Cm_SSNEdit   = 106;
  123.     Cm_SamEdit   = 107;
  124.     Cm_MikeEdit  = 108;
  125.  
  126.   type
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland Pascal/Turbo Pascal           NUMBER  :  1203
  141.   VERSION  :  7.0
  142.        OS  :  All
  143.      DATE  :  December 11, 1992                        PAGE  :  3/7
  144.  
  145.     TITLE  :  Using Validators in OWL Applications
  146.  
  147.  
  148.  
  149.  
  150.     TMyApplication = object(TApplication)
  151.       procedure InitMainWindow; virtual;
  152.     end;
  153.  
  154.   type
  155.     PMyWindow = ^TMyWindow;
  156.     TMyWindow = object(TWindow)
  157.       constructor Init(AParent: PWindowsObject; AName: PChar);
  158.       procedure cmDialog(var Msg: TMessage);
  159.         virtual cm_First + cm_Dialog;
  160.     end;
  161.  
  162.     PMyEdit = ^TMyEdit;
  163.     TMyEdit = Object(TEdit)
  164.       procedure WMKillFocus(var Msg: TMessage);
  165.         virtual  wm_First + wm_KillFocus;
  166.       procedure WmGetDlgCode(var Msg: TMessage);
  167.         virtual  wm_First + wm_GetDlgCode;
  168.     end;
  169.  
  170.     PMyDialog = ^TMyDialog;
  171.     TMyDialog = Object(TDialog)
  172.       constructor Init(AParent: PWindowsObject; AName: PChar);
  173.     end;
  174.  
  175.     PSamValidator = ^TSamValidator;
  176.     TSamValidator = object(TPxPictureValidator)
  177.       function IsValid(const S: string): Boolean; virtual;
  178.       procedure Error; virtual;
  179.     end;
  180.  
  181.   procedure TMyEdit.WMKillFocus(var Msg: TMessage);
  182.   begin
  183.     DefWndProc(Msg)
  184.   end;
  185.  
  186.   procedure TMyEdit.WMGetDlgCode(var Msg: TMessage);
  187.   begin
  188.     DefWndProc(Msg);
  189.   end;
  190.  
  191.   {--------------------------------------------------}
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland Pascal/Turbo Pascal           NUMBER  :  1203
  207.   VERSION  :  7.0
  208.        OS  :  All
  209.      DATE  :  December 11, 1992                        PAGE  :  4/7
  210.  
  211.     TITLE  :  Using Validators in OWL Applications
  212.  
  213.  
  214.  
  215.  
  216.   { TMyWindow's method implementations:              }
  217.   {--------------------------------------------------}
  218.  
  219.  
  220.   constructor TMyWindow.Init(AParent: PWindowsObject;
  221.                     AName: PChar);
  222.   begin
  223.     TWindow.Init(AParent, AName);
  224.     Attr.Menu := LoadMenu(HInstance, 'Menu_1');
  225.   end;
  226.  
  227.   procedure TMyWindow.cmDialog(var Msg: TMessage);
  228.   var
  229.     D: PDialog;
  230.   begin
  231.     D := New(PMyDialog, Init(@Self, 'Dialog_1'));
  232.     Application^.ExecDialog(D);
  233.   end;
  234.  
  235.   {--------------------------------------------------}
  236.   { TMyWindow's method implementations:              }
  237.   {--------------------------------------------------}
  238.  
  239.   constructor TMyDialog.Init(AParent: PWindowsOBject;
  240.                              AName: PChar); var
  241.     AEdit: PEdit;
  242.     MyEdit: PMyEdit;
  243.   begin
  244.     TDialog.Init(AParent, AName);
  245.     AEdit := New(PEdit, InitResource(@Self, Cm_NumEdit, 50));
  246.     AEdit^.SetValidator(New(PRangeValidator, Init(5, 8)));
  247.     MyEdit := New(PMyEdit, InitResource(@Self, Cm_NumEdit2, 10));
  248.     MyEdit^.SetValidator(New(PRangeValidator, Init(1, 10)));
  249.     AEdit := New(PEdit, InitResource(@Self, Cm_DateEdit, 50));
  250.     AEdit^.SetValidator(New(PPXPictureValidator,
  251.                         Init('{#[#]}/{#[#]}/{##[##]}', True)));
  252.     AEdit := New(PEdit, InitResource(@Self, Cm_StockEdit, 50));
  253.     AEdit^.SetValidator(New(PPXPictureValidator,
  254.                         Init('&&##-####', True)));
  255.     AEdit := New(PEdit, InitResource(@Self, Cm_SSNEdit, 50));
  256.     AEdit^.SetValidator(New(PPXPictureValidator,
  257.                         Init('###-##-####', True)));
  258.     AEdit := New(PEdit, InitResource(@Self, Cm_SamEdit, 50));
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland Pascal/Turbo Pascal           NUMBER  :  1203
  273.   VERSION  :  7.0
  274.        OS  :  All
  275.      DATE  :  December 11, 1992                        PAGE  :  5/7
  276.  
  277.     TITLE  :  Using Validators in OWL Applications
  278.  
  279.  
  280.  
  281.  
  282.     AEdit^.SetValidator(New(PSamValidator, Init('&&&#', True)));
  283.     AEdit := New(PEdit, InitResource(@Self, Cm_MikeEdit, 50));
  284.     AEdit^.SetValidator(New(PPXPictureValidator,
  285.                         Init('Mike#', True)));
  286.   end;
  287.  
  288.   {--------------------------------------------------}
  289.   { TSamValidator's method implementations:  }
  290.   {--------------------------------------------------}
  291.  
  292.   function IsNumber(Chr: Char): Boolean; near; assembler;
  293.   asm
  294.           XOR     AL,AL
  295.           MOV     Ch,Chr
  296.           CMP     Ch,'0'
  297.           JB      @@1
  298.           CMP     Ch,'9'
  299.           JA      @@1
  300.           INC     AL
  301.   @@1:
  302.   end;
  303.  
  304.   function TSamValidator.IsValid(const S: string): Boolean;
  305.   var
  306.     S1,S2: array[0..5] of Char;
  307.   begin
  308.     StrPCopy(S2, S);
  309.     IsValid := False;
  310.     StrLCopy(S1, S2, 3);
  311.     if StrIComp(S1, 'Sam') = 0 then IsValid := True
  312.     else Exit;
  313.     if IsNumber(S2[4]) then IsValid := True;
  314.   end;
  315.  
  316.   procedure TSamValidator.Error;
  317.   begin
  318.     MessageBox(0,
  319.     'Enter the word Sam followed by a number. Example: Sam1',
  320.     nil, mb_IconExclamation + mb_OK);
  321.   end;
  322.  
  323.   {---------------------------------------------------}
  324.   { TMyApplication's method implementations:          }
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.   PRODUCT  :  Borland Pascal/Turbo Pascal           NUMBER  :  1203
  339.   VERSION  :  7.0
  340.        OS  :  All
  341.      DATE  :  December 11, 1992                        PAGE  :  6/7
  342.  
  343.     TITLE  :  Using Validators in OWL Applications
  344.  
  345.  
  346.  
  347.  
  348.   {---------------------------------------------------}
  349.   procedure TMyApplication.InitMainWindow;
  350.   begin
  351.     MainWindow := New(PMyWindow,
  352.       Init(nil, 'Sample ObjectWindows Program'));
  353.   end;
  354.  
  355.  
  356.   {--------------------------------------------------}
  357.   { Main program:                                    }
  358.   {--------------------------------------------------}
  359.  
  360.   var
  361.     MyApp: TMyApplication;
  362.  
  363.   begin
  364.     MyApp.Init('MyProgram');
  365.     MyApp.Run;
  366.     MyApp.Done;
  367.   end.
  368.  
  369.  
  370.  
  371.   MENU_1 MENU
  372.   BEGIN
  373.        MENUITEM "E&xit", 24340
  374.        MENUITEM "&Dialog", 101
  375.   END
  376.  
  377.  
  378.   DIALOG_1 DIALOG 37, 28, 196, 163
  379.    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION |
  380.   WS_SYSMENU CLASS "BorDlg"
  381.   CAPTION "Validator Dialog"
  382.   BEGIN
  383.        CONTROL "", 102, "EDIT", ES_LEFT | WS_CHILD | WS_VISIBLE |
  384.   WS_BORDER | WS_TABSTOP, 124, 12, 59, 12
  385.        CONTROL "", 103, "EDIT", ES_LEFT | WS_CHILD | WS_VISIBLE |
  386.   WS_BORDER | WS_TABSTOP, 123, 29, 59, 12
  387.        CONTROL "", 104, "EDIT", ES_LEFT | WS_CHILD | WS_VISIBLE |
  388.   WS_BORDER | WS_TABSTOP, 123, 46, 59, 12
  389.        CONTROL "", 105, "EDIT", ES_LEFT | WS_CHILD | WS_VISIBLE |
  390.   WS_BORDER | WS_TABSTOP, 123, 63, 59, 12
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.   PRODUCT  :  Borland Pascal/Turbo Pascal           NUMBER  :  1203
  405.   VERSION  :  7.0
  406.        OS  :  All
  407.      DATE  :  December 11, 1992                        PAGE  :  7/7
  408.  
  409.     TITLE  :  Using Validators in OWL Applications
  410.  
  411.  
  412.  
  413.  
  414.        CONTROL "", 106, "EDIT", ES_LEFT | WS_CHILD | WS_VISIBLE |
  415.   WS_BORDER | WS_TABSTOP, 123, 80, 59, 12
  416.        CONTROL "", 107, "EDIT", ES_LEFT | WS_CHILD | WS_VISIBLE |
  417.   WS_BORDER | WS_TABSTOP, 123, 97, 59, 12
  418.        CONTROL "", 108, "EDIT", ES_LEFT | WS_CHILD | WS_VISIBLE |
  419.   WS_BORDER | WS_TABSTOP, 123, 114, 59, 12
  420.        CONTROL "Button", 2, "BorBtn", 1 | WS_CHILD | WS_VISIBLE |
  421.   WS_TABSTOP, 53, 138, 32, 20
  422.        CONTROL "Button", 1, "BorBtn", 1 | WS_CHILD | WS_VISIBLE |
  423.   WS_TABSTOP, 112, 138, 32, 20
  424.        CONTROL "Valid entries are 1-10 (No Tab)", -1, "STATIC",
  425.   SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 19, 12, 101, 10
  426.        CONTROL "Enter a date", -1, "STATIC", SS_LEFT | WS_CHILD |
  427.   WS_VISIBLE | WS_GROUP, 80, 46, 40, 10
  428.        CONTROL "Enter Stock Number (AANN-NNNN)", -1, "STATIC",
  429.   SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 11, 63, 110, 10
  430.        CONTROL "Enter Social Security Num", -1, "STATIC", SS_LEFT |
  431.   WS_CHILD | WS_VISIBLE | WS_GROUP, 34, 80, 86, 8
  432.        CONTROL "Enter SAM followed by number", -1, "STATIC",
  433.   SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 20, 97, 100, 10
  434.        CONTROL "Enter MIKE followed by number", -1, "STATIC",
  435.   SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 17, 114, 103, 10
  436.        CONTROL "", 106, "BorShade", 32774 | WS_CHILD | WS_VISIBLE,
  437.   6, 7, 184, 125
  438.        CONTROL "Valid entries are 5-8 (Tab Ok)", -1, "STATIC",
  439.   SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 19, 29, 96, 10
  440.   END
  441.  
  442.   DISCLAIMER: You have the right to use this technical information
  443.   subject to the terms of the No-Nonsense License Statement that
  444.   you received with the Borland product to which this information
  445.   pertains.
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.